<?php namespace Home\Controller;
use Think\Controller;
require APP_PATH."/Home/Common/functions.php";
class IndexController extends Controller {

    //ThinkPHP的构造函数
    public function _initialize() {
        //自动加载的类
        new \Org\Www_autoload();
        //金贵简介列表
        $newsModel = D("News");
        $about_list = $newsModel->where("ntype=1 and is_show=1")->field('id,title')->select();
        $this->assign("about_list",$about_list);
        //友情链接
        $linkModel = D("Link");
        $link_list = $linkModel->select();
        $this->assign("link_list",$link_list);
        $this->assign('site_name','郴州市金贵银业股份有限公司');
        }

    public function index(){
    	$newsModel = D("News");
    	//首页新闻资讯
    	$top_new = $newsModel->where("ntype=2 and istop=1 and imgurl<>'' and meta_description<>''")->order('id DESC')->find();
    	$index_news = $newsModel->where("ntype=2 and istop=1 and id<>'".(int)$top_new['id']."'")->order('id DESC')->limit(0,6)->select();
    	$this->assign("top_new",$top_new);
    	$this->assign("index_news",$index_news);
    	//首页金贵简介
        $about = $newsModel->where("ntype=1 and istop=1 and is_show=1")->find();
        $this->assign("about",$about);
        //首页视频
        $index_video_url=M('Video')->where('is_show=1 and video_type=1')->order('sort desc,id desc')->limit(0,1)->getField('imgurl');
        $this->assign("index_video_url",$index_video_url);

    	//首页相册
    	$albumModel = D("Album");
    	$album_list = $albumModel->where("is_show=1")->order("sort ASC,add_time DESC")->limit("0","8")->select();
    	$this->assign("album_list",$album_list);
    	//首页招聘岗位
    	$positionsModel = D("Positions");
    	$positions_list = $positionsModel->where("status=1 and class=1")->order("update_time DESC")->limit(0,7)->select();
    	$this->assign("positions_list",$positions_list);
    	
    	$this->display();
    }

    public function concept_content(){
        $this->display();
    }

    public function vip(){
        
        $this->display();
    }

    public function recruit_jobs(){

        if(I("page")){
            $page = I("page");
        }else{
            $page = 1;
        }

        $position= D("Positions");
        $list= $position->order('update_time desc')->where("class=1 and status = 1")->limit(($page-1)*8,8)->select();
        $count= $position->where("class=1 and status = 1")->count();

        $this->assign('list', $list);
        $this->assign('count', ceil($count/8));
        $this->assign('page', $page);
        $this->display();
    }

	public function job_list(){
     $this->redirect('/recruit_jobs');
	}

    public function recruit_jobs_sh(){
        if(I("page")){
            $page = I("page");
        }else{
            $page = 1;
        }

        $position= D("Positions");
        $list= $position->order('update_time desc')->where("class=2 and status = 1")->limit(($page-1)*8,8)->select();
        $count= $position->where("class=2 and status = 1")->count();

        $this->assign('list', $list);
        $this->assign('count', ceil($count/8));
        $this->assign('page', $page);
        $this->display();
    }

    //企业相册 分类列表
    public function album(){
        $product = D("AlbumCategory");
        if(I("page")){
            $page = I("page");
        }else{
            $page = 1;
        }
        $cid=I("get.cid");
        if($cid==''){
            $cid=1;
        }
        $perpage=4; //每页显示数
        $list= $product->where("is_show=1")->limit(($page-1)*$perpage,$perpage)->select();
        //dump($list);exit;
        $count= $product->where("is_show=1")->count();
        //echo $count;exit;
        $this->assign('list',$list);
        $this->assign('count', ceil($count/$perpage));
        $this->assign('page',$page);
        $this->assign('cid',$cid);
        $this->display();
    }

    //企业相册 相册中图片列表
    public function album_list(){
        $product = D("Album");
        if(I("page")){
            $page = I("page");
        }else{
            $page = 1;
        }
        $cid=I("get.cid");
        if($cid==''){
            $cid=1;
        }
        $perpage=4; //每页显示数
        $list= $product->where("is_show=1 and category_id=$cid")->limit(($page-1)*$perpage,$perpage)->select();
        //dump($list);exit;
        $count= $product->where("is_show=1 and category_id=$cid")->count();
        //echo $count;exit;
        $this->assign('list',$list);
        $this->assign('count', ceil($count/$perpage));
        $this->assign('page',$page);
        $this->assign('cid',$cid);
        $this->display();
    }

    //相册详情
    public function album_detail(){
        $cid=I("get.cid");
        $id=I("get.id");
        if($cid==''){
            $cid=1;
        }
        $this->assign('cid',$cid);
        $productModel = D("Album");
        if($id==''){
            $id=$productModel->where('category_id='.$cid.' and is_show=1 ')->order('id desc')->limit(0,1)->getField('id');
        }
        if($id){
            $product = $productModel->where('id='.$id)->find();
            $this->assign('product',$product);
            $productModel->where('id='.$id)->setInc('hit');
        }
        //上一条
        $last_id=$productModel->where('category_id='.$cid.' and is_show=1 and id<'.$id)->order('id desc')->limit(0,1)->getField('id');
        //下一条
        $next_id=$productModel->where('category_id='.$cid.' and is_show=1 and id>'.$id)->order('id asc')->limit(0,1)->getField('id');
        //echo $next_id;exit;
        $this->assign('last_id',$last_id);
        $this->assign('next_id',$next_id);
        $this->display();
    }

    /*//企业文化 相册
    public function album(){

        if(I("page")){
            $page = I("page");
        }else{
            $page = 1;
        }
        $perpage=6; //每页显示数
        $Model= D("Album");
        $list= $Model->order('sort desc,create_time desc,id desc')->where("is_show=1")->limit(($page-1)*$perpage,$perpage)->select();
        //dump($list);exit;
        $count= $Model->where("is_show=1")->count();

        $this->assign('list', $list);
        $this->assign('count', ceil($count/$perpage));
        $this->assign('page', $page);
        $this->display();
    }

    //企业文化 相册详情页
    public function album_detail(){
        header("Content-type: text/html; charset=utf-8");
        if(I("id")){
            $id=intval(I("id"));
        }else{
            $id=1;
        }
        $this->assign('id', $id);
        $Model= D("Album");
        $list= $Model->order('sort desc,create_time desc,id desc')->where("is_show=1")->field('id')->select();
        //dump($list);exit;
        foreach($list as $k=>$v){
            if($v['id']==$id){
                $key=$k;
            }
        }
        $info=$Model->where('id='.$id.' and is_show=1')->field('name,create_time,hit,imgurl')->find();
        //dump($info);exit;
        $this->assign('info', $info);
        //上一条
        if(is_array($list[$key+1])){
            $last_info=$Model->where('id='.$list[$key+1]['id'].' and is_show=1')->field('name')->find();
            $last_info['id']=$list[$key+1]['id'];
            $this->assign('last_info', $last_info);
        }
        //下一条
        if(is_array($list[$key-1])){
            $next_info=$Model->where('id='.$list[$key-1]['id'].' and is_show=1')->field('name')->find();
            $next_info['id']=$list[$key-1]['id'];
            $this->assign('next_info', $next_info);
        }
        //dump($next_info);exit;
        $this->display();
    }*/

    //企业文化 公益活动
    public function charity(){
        $newsModel = D("News");
        if(I("page")){
            $page = I("page");
        }else{
            $page = 1;
        }
        $perpage=6; //每页显示数
        $list= $newsModel->where("is_show=1 and ntype=3")->order('descno desc,newsdt desc,id desc')->limit(($page-1)*$perpage,$perpage)->select();
        $count= $newsModel->where("is_show=1 and ntype=3")->count();
        $this->assign('list',$list);
        $this->assign('count', ceil($count/$perpage));
        $this->assign('page',$page);
        $this->display();
    }
    //企业文化 公益活动详情
    public function charity_detail(){
        $newsModel = D("News");
        $id=I('get.id');
        $this->assign('id', $id);
        $Model= D("News");
        $list= $Model->order('descno desc,newsdt desc,id desc')->where("is_show=1 and ntype=3")->field('id')->select();
        //dump($list);exit;
        foreach($list as $k=>$v){
            if($v['id']==$id){
                $key=$k;
            }
        }
        $info=$Model->where('id='.$id.' and is_show=1')->field('title,newsdt,hit,content')->find();
        //dump($info);exit;
        $this->assign('info', $info);
        //上一条
        if(is_array($list[$key+1])){
            $last_info=$Model->where('id='.$list[$key+1]['id'].' and is_show=1')->field('title')->find();
            $last_info['id']=$list[$key+1]['id'];
            $this->assign('last_info', $last_info);
        }
        //下一条
        if(is_array($list[$key-1])){
            $next_info=$Model->where('id='.$list[$key-1]['id'].' and is_show=1')->field('title')->find();
            $next_info['id']=$list[$key-1]['id'];
            $this->assign('next_info', $next_info);
        }
        //dump($next_info);exit;
        //更新统计数
        $newsModel->where('id='.$id)->setInc('hit',1);
        $this->display();

    }

    //系统文章统一控制器
    public function about(){
        if(I("id")){
            $id=intval(I("id"));
        }else{
            $id=1;
        }
        $this->assign('id', $id);
        $info=M('News')->where('id='.$id.' and is_show=1')->find();
        //dump($info);exit;
        $this->assign('info', $info);
        $this->display();
    }

    public function recruit_jobs_detail(){
        if(I("pos_id")){
            $pos_id = I("pos_id");
        }else{
            $pos_id = 1;
        }

        $position= D("Positions");
        $position_info= $position->where("pos_id=".$pos_id)->find();

        $this->assign('position_info', $position_info);
        $this->display();
    }

    public function apply_jobs(){

        $position_apply_data = I();

        define('ROOT_PATH', dirname(THINK_PATH) .'/www/' );

        if(!empty($_FILES['resume']['tmp_name'])){

            $root_path=ROOT_PATH.'Public/Uploads/resume/';
            if(!is_dir($root_path)){
                mkdir($root_path);
            }

            //上传图片
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     11113145728 ;// 设置附件上传大小
            $upload->exts      =     array("doc","zip","rar","txt");// 设置附件上传类型
            $upload->rootPath  =     $root_path.'/'; // 设置附件上传根目录
            $upload->savePath  =     ''; // 设置附件上传（子）目录
            $upload->subName='';
            $upload->saveName='uniqid';
            $info   =   $upload->upload();

            if(!$info) {// 上传失败
                echo json_encode("false");exit;
            }else{
                $position_apply_data['resume'] = '/Public/Uploads/resume/'.$info['resume']['savename'];
                $PositionApply= D("PositionApply");
                if($PositionApply->add($position_apply_data)){
                    echo json_encode(true);
                }else{
                    echo json_encode(false);
                }
            }
        }
    }

    public function contact_us(){
        $this->display();
    }

    public function show_verify(){
        $Verify =     new \Think\Verify();
        $Verify->imageW = 100;  //图片宽
        $Verify->imageH = 24;  //图片高
        $Verify->fontSize =12;  //字体大小
        $Verify->length   = 4;  //字符数
        $Verify->fontttf = '5.ttf';  //字体
        $Verify->expire = 30;  //有效期
        $Verify->useNoise = false;  //不添加杂点
        $Verify->entry();
    }

    public function message_board(){
        $this->display();
    }

    public function message_board_add(){
        header("Content-Type: text/html; charset=UTF-8");

        //验证码验证
        $verify = new \Think\Verify();
        $passcode=addslashes($_POST['passcode']);
        if($verify->check($passcode, '')==false){
            echo "<script>alert('验证码错误');history.back(-1);</script>";exit;
        }else{
            $contact_data = I();
            $contact_data['add_time'] = date('Y-m-d H:i:s');
            $Contact = M("Contact");
            if($Contact->add($contact_data)){
                echo "<script>alert('留言成功');window.location.href='/message_board'</script></script>";exit;
            }
        }
    }

    //企业文化 视频
    public function video(){

        if(I("page")){
            $page = I("page");
        }else{
            $page = 1;
        }
        $perpage=6; //每页显示数
        $Model= D("Video");
        $list=$Model->order('sort desc,create_time desc,id desc')->where("is_show=1")->limit(($page-1)*$perpage,$perpage)->select();
        $count=$Model->where("is_show=1")->count();
        //dump($list);exit;
        $this->assign('list', $list);
        $this->assign('count', ceil($count/$perpage));
        $this->assign('page', $page);
        $this->display();
    }

    //企业文化 视频详情页
    public function video_detail(){
        header("Content-type: text/html; charset=utf-8");
        if(I("id")){
            $id=intval(I("id"));
        }else{
            $id=1;
        }
        $this->assign('id', $id);
        $Model= D("Video");
        $list= $Model->order('sort desc,create_time desc,id desc')->where("is_show=1")->field('id')->select();
        //dump($list);exit;
        foreach($list as $k=>$v){
            if($v['id']==$id){
                $key=$k;
            }
        }
        $info=$Model->where('id='.$id.' and is_show=1')->field('name,create_time,hit,imgurl,flashurl,video_type')->find();
        //dump($info);exit;
        $this->assign('info', $info);
        //上一条
        if(is_array($list[$key+1])){
            $last_info=$Model->where('id='.$list[$key+1]['id'].' and is_show=1')->field('name')->find();
            $last_info['id']=$list[$key+1]['id'];
            $this->assign('last_info', $last_info);
        }
        //下一条
        if(is_array($list[$key-1])){
            $next_info=$Model->where('id='.$list[$key-1]['id'].' and is_show=1')->field('name')->find();
            $next_info['id']=$list[$key-1]['id'];
            $this->assign('next_info', $next_info);
        }
        //dump($next_info);exit;
        $this->display();
    }
    
    //资讯列表
    public function news_search(){
    	$newsModel = D("News");
    	if(I("page")){
    		$page = I("page");
    	}else{
    		$page = 1;
    	}
    	$key=I("get.key");
        //echo htmlspecialchars($key);exit;
    	$search_sql = '';
    	if(!empty($key)){
    		$search_sql .= ' and title like \'%'.$key.'%\'';
    	}
    	
    	$perpage=6; //每页显示数
    	$list= $newsModel->field('id,category_id,large_thumb,title,newsdt,meta_description')->where("is_show=1 $search_sql and ntype=2")->limit(($page-1)*$perpage,$perpage)->select();
        //dump($list);exit;
    	$count= $newsModel->where("is_show=1 $search_sql and ntype=2")->count();
        //echo $count;exit;
        foreach($list as $k=>$v){
            $list[$k]['meta_description']=strip_tags($list[$k]['meta_description']);
        }
    	/*for($i=0;$i<$count;$i++){
    		$list[$i]['meta_description']=strip_tags($list[$i]['meta_description']);
    	}*/
    	$this->assign('list',$list);
    	$this->assign('count', ceil($count/$perpage));
    	$this->assign('page',$page);
        $this->assign('search_key',$key);
    	$this->display();
    }


    //资讯列表
    public function news(){
        $newsModel = D("News");
        if(I("page")){
            $page = I("page");
        }else{
            $page = 1;
        }
        $cid=I("get.cid");
        if($cid==''){
            $cid=1;
        }
        $perpage=6; //每页显示数
        $list= $newsModel->field('id,category_id,hit,title,newsdt,content,large_thumb')->where("is_show=1 and category_id=$cid and ntype=2")->order('descno asc,id desc')->limit(($page-1)*$perpage,$perpage)->select();
        //dump($list);exit;
        $count= $newsModel->where("is_show=1 and category_id=$cid and ntype=2")->count();
        foreach($list as $k=>$v){
            $list[$k]['content']=strip_tags($v['content']);
        }
        $this->assign('list',$list);
        $this->assign('count', ceil($count/$perpage));
        $this->assign('page',$page);
        $this->assign('cid',$cid);
        $this->display();
    }
    //资讯详情
    public function news_content(){
        $newsModel = D("News");
        $id=I('get.id');
        if($id){
            $new = $newsModel->where('id='.$id)->find();
            $new_last = $newsModel->where('is_show=1 and ntype=2 and id<'.$id)->order('id desc')->find();
            $new_next = $newsModel->where('is_show=1 and ntype=2 and id>'.$id)->order('id asc')->find();
            $this->assign('new',$new);
            $this->assign('new_last',$new_last);
            $this->assign('new_next',$new_next);
            $newsModel->where('id='.$id)->setInc('hit');
        }
        $this->display();
    }

    //产品体系
    public function product(){
        $product = D("Product");
        if(I("page")){
            $page = I("page");
        }else{
            $page = 1;
        }
        $cid=I("get.cid");
        if($cid==''){
            $cid=1;
        }
        $perpage=4; //每页显示数
        $list= $product->where("is_show=1 and category_id=$cid")->limit(($page-1)*$perpage,$perpage)->select();
        $count= $product->where("is_show=1 and category_id=$cid")->count();
        $this->assign('list',$list);
        $this->assign('count', ceil($count/$perpage));
        $this->assign('page',$page);
        $this->assign('cid',$cid);
        $this->display();
    }
    //产品详情
    public function product_detail(){
        $productModel = D("Product");
        $id=I('get.id');
        if($id){
            $product = $productModel->where('id='.$id)->find();
            $this->assign('product',$product);
            $productModel->where('id='.$id)->setInc('hit');
        }
        $this->display();
    }
    
    public function business(){
    	$this->display();
    }

    //分页
    public function getPage($count, $pagecount, $db, $where, $app){

        $page = new \Think\Page($count , $pagecount);
        $page->setConfig('first','首页');
        $page->setConfig('prev','上一页');
        $page->setConfig('next','下一页');
        $page->setConfig('last','尾页');
        $page->setConfig('theme',
            '
                       %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% 共 %TOTAL_PAGE% 页,
                       <form action='.$app.' method="get">
                        到第 <input name="p" type="text" >页<input class="submit" type="submit" value="确定">
                                     ');
        $show = $page->show();
        $list = $db->where($where)->limit($page->firstRow.','.$page->listRows)->select();
        $this->assign('list',$list);
        $this->assign('page',$show);
        return $list;
    }

}